package com.mine.yyb.filter;

import java.io.IOException;
import java.util.Arrays;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.filter.OncePerRequestFilter;

import com.mine.yyb.modules.sys.vo.UserVO;

/**
 * 
 * <pre>
 * <一句话功能简述>
 * 登录过滤器
 * <功能详细描述>
 * </pre>
 * 
 * @author xuganhua
 * @version [版本号, 2015年5月13日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class LoginFilter extends OncePerRequestFilter
{
    
    private static Logger logger = LoggerFactory.getLogger(LoginFilter.class);
    
    @Override
    protected void doFilterInternal(HttpServletRequest servletRequest, HttpServletResponse servletResponse,
        FilterChain paramFilterChain)
        throws ServletException, IOException
    {
        HttpSession session = servletRequest.getSession(true);
        String contextPath = servletRequest.getServletContext().getContextPath();
        String pass[] = {contextPath, 
            contextPath + "/", 
            contextPath + "/admin.html", 
            contextPath + "/html/success.html",
            contextPath + "/html/redirect.html", 
            contextPath + "/html/confirm.html", 
            contextPath + "/recoveryConfirm.html", 
            contextPath + "/sys/user/logout.do",
            contextPath + "/sys/user/checkEmailExist.do", 
            contextPath + "/sys/user/admin.do",
            contextPath + "/sys/user/pwdRecovery.do",
            contextPath + "/sys/notice/queryTheNotice.do",
            
            contextPath + "/initAdminLoginPage.do",
            contextPath + "/admin.do", 
            contextPath + "/initHomePage.do",
            contextPath + "/login.do", 
            contextPath + "/initLoginPage.do",
            contextPath + "/initRegisterPage.do",
            contextPath + "/checkUsername.do", 
            contextPath + "/checkEmailExist.do",
            contextPath + "/checkEmail.do",
            contextPath + "/register.do", 
            contextPath + "/reSendEmail.do", 
            contextPath + "/activate.do",
            contextPath + "/initRecoveryPage.do",
            };
        String currentUri = servletRequest.getRequestURI();
        logger.info("currentUri = " + currentUri);
        // 有些路径是不可以过滤的，
        if (Arrays.asList(pass).contains(currentUri))
        {
            paramFilterChain.doFilter(servletRequest, servletResponse);
        }
        else
        {
            // 判断是否已经登陆、或者session是否已经过期
            UserVO userInfoVO = (UserVO)session.getAttribute("userToken");
            if (userInfoVO == null)
            {
                servletResponse.sendRedirect(contextPath + "/initLoginPage.do");
            }
            else
            {
                paramFilterChain.doFilter(servletRequest, servletResponse);
            }
        }
    }
}
